<template>
  <Tag v-if="ItemValue == 'order_type'" :state="CurretItem.order_type" name="订单类型" />
  <Tag v-else-if="ItemValue == 'order_operate'" :state="CurretItem.order_operate" name="订单操作" />
  <Tag v-else-if="ItemValue == 'order_style'" :state="CurretItem.order_style" name="订单类型" />
  <Tag v-else-if="ItemValue == 'order_status'" :state="CurretItem.order_status" name="订单状态" />
  <Tag v-else-if="ItemValue == 'ship_style'" :state="CurretItem.ship_style" name="发货状态" />
  <!-- 月份 -->
  <span v-else-if="ItemValue == 'Month'">{{
    CurretItem.order_time | MonthDate
  }}</span>
  <!-- 年份 -->
  <span v-else-if="ItemValue == 'YearDate'">{{
    CurretItem.order_time | YearDate
  }}</span>
  <!-- 下载附件 -->
  <el-button v-else-if="isInitialFile(ItemValue) && CurretItem[ItemValue] != '不需要'" size="mini" type="primary"
    @click="DownloadPdf(CurretItem[ItemValue])">下载附件</el-button>
  <!-- 图片展示 -->
  <el-image v-else-if="isInitialImg(ItemValue) && CurretItem[ItemValue]" style="width: 40px; height: 40px"
    :src="isUrlFromDomain(CurretItem[ItemValue]) ? CurretItem[ItemValue] : 'https://qw.ccpluss.com' + CurretItem[ItemValue]"
    :preview-src-list="[isUrlFromDomain(CurretItem[ItemValue]) ? CurretItem[ItemValue] : 'https://qw.ccpluss.com' + CurretItem[ItemValue]]"></el-image>
  <!-- 商品状态 -->
  <span v-else-if="ItemValue == 'return_status'"> {{ CurretItem[ItemValue] == 1 ? '商品完好，可以二销' : CurretItem[ItemValue] ==
    2 ? '商品破损，需要报废' : '空' }} </span>
  <!-- 数量为空则为0 -->
  <span v-else-if="isInitialNumber(ItemValue)">{{ CurretItem[ItemValue] ? CurretItem[ItemValue] : "0" }}</span>
  <!-- 其余为空则为空 -->
  <span v-else>{{ CurretItem[ItemValue] ? CurretItem[ItemValue] : "空" }}</span>
</template>

<script>
import axios from 'axios'
export default {
  name: "ColumnTable",
  props: {
    CurretItem: {
      type: Object,
      default: function () {
        return {};
      },
    },
    ItemValue: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
    };
  },
  computed: {},
  created() { },
  mounted() { },
  watch: {},
  methods: {
    // 如果这些字段是空 应该为0
    isInitialNumber(ItemValue) {
      const ItemName = ["pay_price", "sku_num", "purchase_price", "settle_price", "new_order_ship_num", "stock_unit_price", "direct_ship_num", "direct_ship_unit_price", "total_ship_num", "sku_total_price", "ship_price", "total_price", "retrun_sku_num", "return_trans", "exchange_order_num", "stock_in_unit_price", "exchangeSkuTotalPrice", "exchange_ship_price", "exchangeTotalPrice", "stock_in_num", "stock_in_total_price", "new_order_ship_total", "exchange_sku_ship_total", "quality_stock", "scrap_stock", "quality_stock_price", 'scrap_stock_price', "return_sku_unit_price", "return_stock_in_total_price", "quality_stock_unit_price", "stock_new_order_sku_num","surplus_num","actual_num"]
      return ItemName.includes(ItemValue);
    },
    // 如果是这些字段,应该为下载附件
    isInitialFile(ItemValue) {
      const ItemName = ["print_PDF", "install_file"]
      return ItemName.includes(ItemValue);
    },
    // 如果这些字段是空 应该为图片
    isInitialImg(ItemValue) {
      const ItemName = ["return_pic"]
      return ItemName.includes(ItemValue);
    },
    // 判断图片是线上还是上传
    isUrlFromDomain(ItemValue) {
      const url = ItemValue;
      const domain = 'wdcdn.qpic.cn'
      // 使用正则表达式匹配 URL 的域名部分
      const regex = new RegExp(`^https://${domain}/`, 'i');
      // 返回匹配结果
      return regex.test(url);
    },
    // 下载附件
    DownloadPdf(file) {
      axios(file, {
        //pdf的位置
        responseType: "blob",
      }).then((res) => {
        const url = window.URL.createObjectURL(new Blob([res.data]));
        const link = document.createElement("a");
        link.href = url;
        let fileName = `附件.pdf`; //保存到本地的文件名称
        link.setAttribute("download", fileName);
        document.body.appendChild(link);
        link.click();
        console.log(link);
        link.remove();
      });
    },
  },
}
</script>

<style scoped lang="scss"></style>
